package com.lucker.service;

import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class InitOpen {

	
	public static void main(String args[]) {
		
		
		try {
			//creatOpenTime("08:46:00", 41);
			
			String sql = "select * from cai_piao";
			ResultSet rs = JdbcUtil.query(sql);
			while(rs.next()) {
				String dm = rs.getString("domain_name");
				String fs = rs.getString("first_issue_time");
				int count = rs.getInt("issue_count");
				String tyid = rs.getString("cai_type_id");
				long cai_id = rs.getLong("cai_id");
				
				List<TempTime> ttlist = creatOpenTime(fs,count);
				
				//System.out.println(dm);
				for(TempTime tt : ttlist) {
					
					String insert = "INSERT INTO `cai_piao_open_time`( `caipiao_type_id`, `caipiao_issueno`, `open_time`, `domain_id`, `domain_name`, `cai_id`) VALUES ('"+tyid+"', '"+tt.getQi()+"', '"+tt.getSj()+"', NULL, '"+dm+"', "+cai_id+")";
					
                   					
					JdbcUtil.exe(insert);
					
				}
				
				System.out.println(dm);
				
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	
	
		/**
		 * 将HH:mm:ss时间格式转换成分钟数
		 * @param time
		 * @return 不足一分钟算一分钟
		 */
		public static int getMinutesTime(String time){
		    SimpleDateFormat sdf= new  SimpleDateFormat("HH:mm:ss");

		    try {
		        Date start = sdf.parse(time);
		        int hours = start.getHours();
		        int minutes = start.getMinutes();
		        int seconds = start.getSeconds();
		        if (hours!=0){
		            int hM = hours * 60;
		            if(seconds!=0){
		                int lastTime=hM+minutes+1;
		                return lastTime;
		            }else
		            {
		                int lastTime=hM+minutes;
		                return lastTime;
		            }
		        }else{
		            if(seconds!=0){
		                int lastTime=minutes+1;
		                return lastTime;
		            }else
		            {
		                int lastTime=minutes;
		                return lastTime;
		            }
		        }
		    } catch (ParseException e) {
		        e.printStackTrace();
		    }

		    return 0;
		}

		// firstTime格式08：06：00
		public static List<TempTime> creatOpenTime(String firstTime, int issueCount){
		    int initTime = getMinutesTime(firstTime);
		    List<TempTime> returnList = new ArrayList<TempTime>();
		    for (int i=0;i<issueCount;i++){
		        int tempTime = initTime + 20*i;
		        if(tempTime>=24*60){
		            tempTime = tempTime - 24*60;
		        }
		        StringBuilder stringBuilder = new StringBuilder();
		        stringBuilder.append(CaiyunConfig.sDecimalFormat1.format(tempTime/60)).append(":").append(CaiyunConfig.sDecimalFormat1.format(tempTime%60)).append(":00");
		        String qi = (i+1) > 9 ? "" + (i + 1) : "0" + (i + 1) ;
		        //System.out.println("---creatOpenTime---"+"caipiao_issueno="+qi+";open_time="+stringBuilder.toString());
		        TempTime tt = new TempTime();
		        tt.setQi(qi);
		        tt.setSj(stringBuilder.toString());
		        returnList.add(tt);
		    }
		    
		    return returnList;
		}

}
